﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

using CPPEI.Extensions;

namespace CPPEI.WebApi
{
    public static class ConfigDocument
    {
        /// <summary>
        /// 从配置文件中读取配置对象
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static Config Load(string filePath)
        {
            Config config = new Config();
            config.AuthMode = "Cookie";
            config.AuthProvider = new CookieAuthProvider();

            if (!File.Exists(filePath))
            {
                return config;
            }

            XmlDocument xDoc = XmlDocExtension.GetDocument(filePath);
            if (xDoc == null)
            {
                return config;
            }

            XmlNode xWebApi = xDoc.GetNode("WebApi");

            XmlNodeList xSettingItems = xWebApi.GetNode("Settings").GetNodes("Add");
            if (xSettingItems == null)
            {
                return config;
            }

            foreach (XmlNode xSettingItem in xSettingItems)
            {
                string key = xSettingItem.GetAttributeValue("Key");
                string value = xSettingItem.GetAttributeValue("Value");

                if (string.Equals(key, "IsCamelCasePropertyName"))
                {
                    config.IsCamelCasePropertyName = value.Convert(true);
                }
                else if (string.Equals(key, "AuthMode"))
                {
                    config.AuthMode = value;
                    if (string.Equals(value, "Cookie"))
                    {
                        config.AuthProvider = new CookieAuthProvider();
                    }
                    else
                    {
                        config.AuthProvider = new FormsAuthProvider();
                    }
                }
            }

            XmlNodeList xModuleItems = xWebApi.GetNode("Modules").GetNodes("Module");
            if (xModuleItems != null)
            {
                #region foreach modules
                foreach (XmlNode xModuleItem in xModuleItems)
                {
                    string assembly = xModuleItem.GetAttributeValue("Assembly");
                    string type = xModuleItem.GetAttributeValue("Type");
                    string name = xModuleItem.GetAttributeValue("Name");

                    if (!string.IsNullOrEmpty(assembly) 
                        && !string.IsNullOrEmpty(type)
                        && !string.IsNullOrEmpty(name))
                    {
                        Config.Module module = new Config.Module();
                        module.Assembly = assembly;
                        module.Type = type;
                        module.Name = name;

                        config.Modules.Add(module);

                        //XmlNodeList xActionItems = xModuleItem.GetNode("Actions").GetNodes("Action");
                        //if (xActionItems != null)
                        //{
                        //    foreach (XmlNode xActionItem in xActionItems)
                        //    {
                        //        Config.Action action = new Config.Action();
                        //        action.Name = xActionItem.GetAttributeValue("Name");
                        //        action.Scenario = xActionItem.GetAttributeValue("Scenario");
                        //        action.Type = xActionItem.GetAttributeValue("Type");
                        //        module.Actions.Add(action);
                        //    }
                        //}
                    }
                }
                #endregion
            }

            return config;
        }

        /// <summary>
        /// 将配置对象保存到文件中
        /// </summary>
        /// <param name="config"></param>
        /// <param name="filePath"></param>
        public static bool Save(Config config, string filePath)
        {
            XmlDocument xDoc = new XmlDocument();
            XmlNode xWebApi = xDoc.SetNode("WebApi");
            XmlNode xSettings = xWebApi.SetNode("Settings");

            XmlNode xAuthMode = xSettings.AppendNode("Add");
            xAuthMode.SetAttribute("Key", "AuthMode");
            xAuthMode.SetAttribute("Value", config.AuthMode);

            XmlNode xIsCamelCasePropertyName = xSettings.AppendNode("Add");
            xIsCamelCasePropertyName.SetAttribute("Key", "IsCamelCasePropertyName");
            xIsCamelCasePropertyName.SetAttribute("Value", config.IsCamelCasePropertyName.ToString());

            XmlNode xModules = xWebApi.SetNode("Modules");
            foreach (Config.Module module in config.Modules)
            {
                XmlNode xModuleItem = xModules.SetNode("Module");
                xModuleItem.SetAttribute("Assembly", module.Assembly);
                xModuleItem.SetAttribute("Type", module.Type);
                xModuleItem.SetAttribute("Name", module.Name);

                //XmlNode xActions = xModuleItem.SetNode("Actions");
                //foreach (Config.Action action in module.Actions)
                //{
                //    XmlNode xActionItem = xActions.AppendNode("Action");
                //    xActionItem.SetAttribute("Name", action.Name);
                //    xActionItem.SetAttribute("Scenario", action.Scenario);
                //    xActionItem.SetAttribute("Type", action.Type);
                //}
            }

            try
            {
                xDoc.Save(filePath);
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
    }
}
